//
//  DashboardViewController.m
//  misDeseos
//
//  Created by Roger Plana Casadella on 11/11/11.
//  Copyright (c) 2011 Champions Freak. All rights reserved.
//

#import "DashboardViewController.h"
#import "MainViewController.h"
#import "AlertPrompt.h"

@implementation DashboardViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        // Custom initialization
    }
    return self;
}

#pragma mark - Actions

- (IBAction)MisListasAction:(id)sender
{
    [mvc LoadMisListas:self];
}
- (IBAction)SusListasAction:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                    message:@"Esta funcionalidad no esta implementada aún"
                                                   delegate:nil 
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    
    [alert show];
    [alert release];
}
- (IBAction)QRAction:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                    message:@"Esta funcionalidad no esta implementada aún"
                                                   delegate:nil 
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    
    [alert show];
    [alert release];
}
- (IBAction)CodeAction:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                    message:@"Esta funcionalidad no esta implementada aún"
                                                   delegate:nil 
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    
    [alert show];
    [alert release];
    /*
    AlertPrompt *prompt = [AlertPrompt alloc];
    prompt = [prompt initWithTitle:@"Introduce el codigo" message:@"Please enter some text in" delegate:self cancelButtonTitle:@"Cancelar" okButtonTitle:@"Enviar"];
    [prompt show];
    [prompt release];
    */
}

#pragma mark - ASIHTTPRequest Delegate Methods

-(void)processResponse:(NSString *)response
{
    //Treiem el monitor d'activitat
    [DSBezelActivityView removeViewAnimated:NO];
    
    
    NSMutableDictionary *loginData = [response JSONValue];
    NSString *responseCode = [loginData objectForKey:@"type"];
    if ([responseCode isEqualToString:@"response"])
    {
        NSString *stat = [loginData objectForKey:@"stat"];
        if ([stat isEqualToString:@"ok"])
        {
            /*
            NSMutableDictionary *respData = [loginData objectForKey:@"data"];
            [mvc setUserName:[respData objectForKey:@"name"]];
            [mvc setHashID:[respData objectForKey:@"hash"]];
            */
            
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:@"El nombre de usuario i password no son correctos"
                                                           delegate:self 
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            
            [alert show];
            [alert release];
        }
    }
    else
    {
        NSString *errorStr = [loginData objectForKey:@"msn"];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR"
                                                        message:errorStr
                                                       delegate:self 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        
        [alert show];
        [alert release];
    }
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    [self processResponse:[request responseString]];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    [DSBezelActivityView removeViewAnimated:YES];
    if ([error code] == 2)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR"
                                                        message:@"The request timed out, try again later"
                                                       delegate:self 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        
        [alert show];
        [alert release];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR"
                                                        message:@"There has been an error with the server, pleas try again later"
                                                       delegate:self 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        
        [alert show];
        [alert release]; 
    }
    NSLog(@"ERROR --> %@",[error localizedDescription]);
}

#pragma mark - Alert Delegates

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != [alertView cancelButtonIndex])
    {
        NSString *entered = [(AlertPrompt *)alertView enteredText];
        NSLog(@"%@",[NSString stringWithFormat:@"You typed: %@", entered]); 
    }
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    mvc = [MainViewController sharedMainViewController];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    [super dealloc];
}

@end
